package com.jml.fallback;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

//定义服务降级处理器
@RestController
public class GatewayFallbackController {
    //定义服务降级处理方法
    @RequestMapping("/fallback")
    public Map<String, Object> defaultfallback() {
        System.out.println("降级操作...");
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "服务超时降级");
        map.put("data", null);
        return map;
    }

    /*@Bean
    public RouteLocator someRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(ps -> ps.path("/**")
                .filters(fs -> fs.circuitBreaker(
                                config -> {
                                    config.setName("myCircuitBreaker");
                                    config.setFallbackUri("forward:/fallback");//指定了跳转的url是fallback
                                }
                                )
                )
                .uri("http://localhost:8080")
                .id("circuitBreaker_filter"))
                .build();
    }*/
}